package com.alibaba.citrus.cr.unified.inventory.facade.service;

import com.alibaba.citrus.common.util.Lists;
import com.alibaba.citrus.cr.common.annotation.FacadeInvoker;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crb2btradecenter.enums.OrderStatus;
import com.epoch.app.crunifiedinventory.inventoryverifyquery.service.InventoryVerifyQueryService;
import com.epoch.app.crunifiedinventory.inventoryverifyrecord.model.InventoryVerifyRecord;
import com.epoch.app.crunifiedinventory.inventoryverifyrecord.service.InventoryVerifyRecordService;
import com.epoch.app.crunifiedinventory.inventoryverifyrecorddetail.model.InventoryVerifyRecordDetail;
import com.epoch.app.crunifiedinventory.inventoryverifyrecorddetail.service.InventoryVerifyRecordDetailService;
import com.epoch.app.crunifiedinventory.model.dto.InventoryVerifyRecordDTO;
import com.epoch.app.crunifiedinventory.model.dto.InventoryVerifyRecordDetailDTO;
import com.epoch.app.crunifiedinventory.model.dto.InventoryVerifyRecordDetailLoadListRequest;
import com.epoch.app.crunifiedinventory.model.dto.InventoryVerifyRecordLoadListRequest;
import com.epoch.app.crunifiedinventory.model.dto.InventoryVerifyRecordQueryRequest;
import com.epoch.app.crunifiedinventory.model.enums.FailReasonEnum;
import groovy.util.StringTestUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @description: RPA对账读服务
 * @author: yuhw
 * @date: 2022年11月23日 19:52
 **/

@Service
@Primary
public class InventoryVerifyQueryServiceImpl implements InventoryVerifyQueryService {

    @Resource
    private InventoryVerifyRecordService inventoryVerifyRecordService;
    @Resource
    private InventoryVerifyRecordDetailService inventoryVerifyRecordDetailService;
    private static final int VERIFY_SUCCESS = 1;
    private static final int VERIFY_FAIL = 0;
    private static final String SUCCESS = "成功";
    private static final String FAIL = "失败";

    /**
     * 查询RPA对账记录列表
     * @param inventoryVerifyRecordQueryRequest
     * @return
     */
    @Override
    @FacadeInvoker(value = "RPA对账记录查询", errorCode = "OTS-03-005-00-15-101")
    public Result<List<InventoryVerifyRecordDTO>> queryInventoryVerifyRecordList(InventoryVerifyRecordQueryRequest inventoryVerifyRecordQueryRequest) {
        List<InventoryVerifyRecordDTO> recordDTOS = Lists.newArrayList();
        InventoryVerifyRecordLoadListRequest request = convertQuery(inventoryVerifyRecordQueryRequest);
        Result<List<InventoryVerifyRecord>> listResult = inventoryVerifyRecordService.queryList(request);
        if (listResult == null || CollectionUtils.isEmpty(listResult.getResult())){
            return Result.success(recordDTOS);
        }
        listResult.getResult().forEach(record -> recordDTOS.add(convert(record,inventoryVerifyRecordQueryRequest)));
        Result result = Result.success(recordDTOS);
        result.setTotal(listResult.getTotal());
        return result;
    }

    /**
     * 查询RPA对账记录明细
     * @param inventoryVerifyRecordDetailLoadListRequest
     * @return
     */
    @FacadeInvoker(value = "RPA对账明细查询", errorCode = "OTS-03-005-00-15-102")
    @Override
    public Result<List<InventoryVerifyRecordDetailDTO>> queryInventoryRecordDetailList(InventoryVerifyRecordDetailLoadListRequest inventoryVerifyRecordDetailLoadListRequest) {
        List<InventoryVerifyRecordDetailDTO> detailDTOS = Lists.newArrayList();
        Result<List<InventoryVerifyRecordDetail>> listResult = inventoryVerifyRecordDetailService.loadList(inventoryVerifyRecordDetailLoadListRequest);
        if (listResult == null || CollectionUtils.isEmpty(listResult.getResult())){
            return Result.success(detailDTOS);
        }
        listResult.getResult().forEach(detail -> detailDTOS.add(convert(detail)));
        Result result = Result.success(detailDTOS);
        result.setTotal(listResult.getTotal());
        return result;
    }

    /**
     * 转换查询条件
     * @param queryRequest
     * @return
     */
    private InventoryVerifyRecordLoadListRequest convertQuery(InventoryVerifyRecordQueryRequest queryRequest){
        InventoryVerifyRecordLoadListRequest request = new InventoryVerifyRecordLoadListRequest();
        //条件基本相同
        BeanUtils.copyProperties(queryRequest,request);
        if (StringUtils.isNotBlank(queryRequest.getFailReason())){
            if (FailReasonEnum.WAREHOUSE_NOT_FOUND.getCode().equals(queryRequest.getFailReason())
                || FailReasonEnum.GOODS_NOT_FOUND.getCode().equals(queryRequest.getFailReason())
                || FailReasonEnum.QUANTITY_FORMAT_ILLEGAL.getCode().equals(queryRequest.getFailReason())
                || FailReasonEnum.INVENTORY_NOT_EXIST.getCode().equals(queryRequest.getFailReason())){
                request.setMsgCode(queryRequest.getFailReason());
            }
//            if (FailReasonEnum.GOODS_NOT_FOUND.getCode().equals(queryRequest.getFailReason())){
//
//            }
//            if (FailReasonEnum.QUANTITY_FORMAT_ILLEGAL.getCode().equals(queryRequest.getFailReason())){
//
//            }
//            if(FailReasonEnum.INVENTORY_NOT_EXIST.getCode().equals(queryRequest.getFailReason())){
//
//            }

            if (FailReasonEnum.AVAILABLE_QUANTITY_DIFF.getCode().equals(queryRequest.getFailReason())){
                request.setAvailableQuantityVerifyStatus(VERIFY_FAIL);
                request.setMsgCode(FailReasonEnum.DEFAULT.getCode());
            }
            if (FailReasonEnum.WITHHOLDING_QUANTITY_DIFF.getCode().equals(queryRequest.getFailReason())){
                request.setWithholdingQuantityVerifyStatus(VERIFY_FAIL);
                request.setMsgCode(FailReasonEnum.DEFAULT.getCode());
            }
            if (FailReasonEnum.UNRESTRICTED_QUANTITY_DIFF.getCode().equals(queryRequest.getFailReason())){
                request.setUnrestrictedQuantityVerifyStatus(VERIFY_FAIL);
                request.setMsgCode(FailReasonEnum.DEFAULT.getCode());
            }
            if (FailReasonEnum.FROZEN_QUANTITY_DIFF.getCode().equals(queryRequest.getFailReason())){
                request.setFrozenQuantityVerifyStatus(VERIFY_FAIL);
                request.setMsgCode(FailReasonEnum.DEFAULT.getCode());
            }
            if (FailReasonEnum.INSPECT_QUANTITY_DIFF.getCode().equals(queryRequest.getFailReason())){
                request.setInspectQuantityVerifyStatus(VERIFY_FAIL);
                request.setMsgCode(FailReasonEnum.DEFAULT.getCode());
            }
        }
        return request;
    }

    private InventoryVerifyRecordDTO convert(InventoryVerifyRecord record,InventoryVerifyRecordQueryRequest request){
        InventoryVerifyRecordDTO recordDTO = new InventoryVerifyRecordDTO();
        BeanUtils.copyProperties(record,recordDTO);
        if (StringUtils.isBlank(recordDTO.getVerifyStatusDesc())){
            recordDTO.setVerifyStatusDesc(Objects.equals(VERIFY_SUCCESS,record.getVerifyStatus()) ? SUCCESS : FAIL);
        }
        if (StringUtils.isNotBlank(request.getFailReason())){
            for (FailReasonEnum value : FailReasonEnum.values()) {
                if (value.getCode().equals(request.getFailReason())){
                    recordDTO.setFailReasonDesc(value.getValue());
                }
            }
        }else {
            //没有筛选条件时 先看校验状态 msgCode
            if (!Objects.equals(VERIFY_SUCCESS,record.getVerifyStatus())){
                if (!FailReasonEnum.DEFAULT.getCode().equals(record.getMsgCode())){
                    for (FailReasonEnum value : FailReasonEnum.values()) {
                        if (value.getCode().equals(record.getMsgCode())){
                            recordDTO.setFailReasonDesc(value.getValue());
                        }
                    }
                }else {
                    if (Objects.equals(VERIFY_FAIL,record.getAvailableQuantityVerifyStatus())){
                        recordDTO.setFailReasonDesc(FailReasonEnum.AVAILABLE_QUANTITY_DIFF.getValue());
                    }else if (Objects.equals(VERIFY_FAIL,record.getWithholdingQuantityVerifyStatus())){
                        recordDTO.setFailReasonDesc(FailReasonEnum.WITHHOLDING_QUANTITY_DIFF.getValue());
                    }else if (Objects.equals(VERIFY_FAIL,FailReasonEnum.FROZEN_QUANTITY_DIFF.getCode())){
                        recordDTO.setFailReasonDesc(FailReasonEnum.FROZEN_QUANTITY_DIFF.getValue());
                    }else if (Objects.equals(VERIFY_FAIL,FailReasonEnum.INSPECT_QUANTITY_DIFF.getCode())){
                        recordDTO.setFailReasonDesc(FailReasonEnum.INSPECT_QUANTITY_DIFF.getValue());
                    }else if (Objects.equals(VERIFY_FAIL,FailReasonEnum.UNRESTRICTED_QUANTITY_DIFF.getCode())){
                        recordDTO.setFailReasonDesc(FailReasonEnum.UNRESTRICTED_QUANTITY_DIFF.getValue());
                    }
                }
            }
        }

        return recordDTO;
    }

    private InventoryVerifyRecordDetailDTO convert(InventoryVerifyRecordDetail detail){
        InventoryVerifyRecordDetailDTO detailDTO = new InventoryVerifyRecordDetailDTO();
        BeanUtils.copyProperties(detail,detailDTO);
        for (OrderStatus value : OrderStatus.values()) {
            if (Objects.equals(value.getCode(),detail.getOrderStatus())){
                detailDTO.setOrderStatusDesc(value.getDesc());
            }
            if (Objects.equals(value.getCode(),detail.getLineStatus())){
                detailDTO.setLineStatusDesc(value.getDesc());
            }
        }
        return detailDTO;
    }
}
